/**
 * 
 */
package com.mc.sql;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author w.weizhen
 *
 */
public class Sws_area_visitflow {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// 得到区域ID和户ID信息
		JSONArray datas = readFile();
		
		List<String> sqls = new ArrayList<String>();
		StringBuffer sb = new StringBuffer();
		
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		String insert_into = "INSERT INTO `sws_area_visitflow` (`huId`, `areaId`, `flow`, `day`, `createTime`, `updateTime`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s');";
		
		// 生成日期数据
		for (int i = 0; i < datas.size(); i++) 
		{
			// 得到区域ID和户ID信息
			JSONObject data = datas.getJSONObject(i);
			String huId = data.getString("huId");
			String areaId = data.getString("areaId");
			
			// 生成日期数据和访问量数据
			List<String> days = new ArrayList<String>();
			for (int j = 0; j < 100; j++) 
			{
				LocalDateTime localDateTime = localDateTime();
				String day = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
				
				// 日期是否已经存在
				if(days.contains(day))
				{
					continue;
				}
				
				String sql = String.format(insert_into, huId, areaId, random(), day, createTime, createTime);
				
				// 拼接，添加 换行符
				sb.append(sql+"\n");
				
//				writeFile(sql);
				
//				sqls.add(sql);
				
			}
		}
//		writeFile(sqls.toString());
		
		writeFile(sb.toString());
	}
	
	private static int random()
	{
		int max = 10000;
	    int min = 0;
	    return (int) (Math.floor(Math.random() * (max - min + 1)) + min);
	}
	
	private static LocalDateTime localDateTime()
	{
		int max = 10000;
	    int min = -10000;
	    int num = (int) (Math.floor(Math.random() * (max - min + 1)) + min);
	    return LocalDateTime.now().minusDays(num);
	}
	
	private static JSONArray readFile()
	{
		JSONArray datas = new JSONArray();
		try {
			String data = FileUtils.readFileToString(new File("D:\\hynetic\\tmp\\sws_area_visitflow.txt"), "UTF-8");
			datas = JSON.parseArray(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return datas;
	}
	
	private static void writeFile(String data)
	{
		try {
			FileUtils.writeStringToFile(new File("D:\\hynetic\\tmp\\sws_area_visitflow_Sql.json"), data, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}